//
//  HWReadRecordCell.m
//  嗨文
//
//  Created by 响当当 on 2019/2/22.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWMyReadRecordCell.h"

@interface HWMyReadRecordCell()
@property (nonatomic, strong)UIImageView *bookIconImageView;//书图片
@property (nonatomic, strong)UIImageView *UnReadPointImageView;//提醒点
@property (nonatomic, strong)UILabel *bookTitleLabel;//书名
@property (nonatomic, strong)UILabel *bookAuthorAndChapterLabel;//书作者及阅读到哪一章
@property (nonatomic, strong)UILabel *bookUpdateTimeLabel;//书更新时间
@property (nonatomic, strong)UIButton *addToBook;//添加到书架
@property (nonatomic, strong)UIView *lineView;//下划线
@end

@implementation HWMyReadRecordCell

- (UIImageView *)bookIconImageView
{
    if (!_bookIconImageView)
    {
        _bookIconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15*kUIScaleBasePlus,8*kUIScaleBasePlus,64*kUIScaleBasePlus,72*kUIScaleBasePlus)];
        _bookIconImageView.image = [UIImage imageNamed:@"bookCommon"];
    }
    return _bookIconImageView;
}

- (UIImageView *)UnReadPointImageView
{
    if (!_UnReadPointImageView)
    {
        _UnReadPointImageView = [[UIImageView alloc] initWithFrame:CGRectMake(12*kUIScaleBasePlus,5*kUIScaleBasePlus,8*kUIScaleBasePlus,8*kUIScaleBasePlus)];
        _UnReadPointImageView.image = [UIImage imageNamed:@"refresh_dot"];
    }
    return _UnReadPointImageView;
}

- (UILabel *)bookTitleLabel
{
    if (!_bookTitleLabel)
    {
        _bookTitleLabel = [HWUtilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(self.bookIconImageView.frame)+13*kUIScaleBasePlus,16*kUIScaleBasePlus,SCREEN_WIDTH-83*kUIScaleBasePlus,15*kUIScaleBasePlus)
                                             textAligment:NSTextAlignmentLeft textClor:LCHexColor(0x363636)
                                                 textFont:15*kUIScaleBasePlus];
    }
    return _bookTitleLabel;
}

- (UILabel *)bookAuthorAndChapterLabel
{
    if (!_bookAuthorAndChapterLabel)
    {
        _bookAuthorAndChapterLabel = [HWUtilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(self.bookIconImageView.frame)+13*kUIScaleBasePlus,CGRectGetMaxY(self.bookTitleLabel.frame)+11*kUIScaleBasePlus,SCREEN_WIDTH-83*kUIScaleBasePlus,13*kUIScaleBasePlus)
                                                        textAligment:NSTextAlignmentLeft
                                                            textClor:LCHexColor(0x8F8F8F)
                                                            textFont:12*kUIScaleBasePlus];
    }
    return _bookAuthorAndChapterLabel;
}

- (UIButton *)addToBook
{
    if (!_addToBook)
    {
        _addToBook = [HWUtilties creatButtonWithFrame:CGRectMake(SCREEN_WIDTH-15*kUIScaleBasePlus-70*kUIScaleBasePlus,35*kUIScaleBasePlus,70*kUIScaleBasePlus,32*kUIScaleBasePlus)
                                         textAligment:NSTextAlignmentCenter
                                                 text:@"+ 书架"
                                             textClor:LCHexColor(0xF54337)
                                            buttonBGC:[UIColor clearColor]
                                             textFont:15*kUIScaleBasePlus
                                               target:self
                                               method:@selector(addBook:)];
        _addToBook.layer.masksToBounds = YES;
        _addToBook.layer.cornerRadius = _addToBook.frame.size.height/2;
        _addToBook.layer.borderColor = LCHexColor(0xF54337).CGColor;
        _addToBook.layer.borderWidth = 1;
    }
    return _addToBook;
}


- (UILabel *)bookUpdateTimeLabel
{
    if (!_bookUpdateTimeLabel)
    {
        _bookUpdateTimeLabel = [HWUtilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(self.bookIconImageView.frame)+13*kUIScaleBasePlus, CGRectGetMaxY(self.bookAuthorAndChapterLabel.frame)+11*kUIScaleBasePlus,SCREEN_WIDTH-83*kUIScaleBasePlus,12*kUIScaleBasePlus)
                                                  textAligment:NSTextAlignmentLeft textClor:LCHexColor(0x8F8F8F) textFont:12*kUIScaleBasePlus];
    }
    return _bookUpdateTimeLabel;
}

- (UIView *)lineView
{
    if (!_lineView)
    {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(80*kUIScaleBasePlus,87*kUIScaleBasePlus,280*kUIScaleBasePlus,1*kUIScaleBasePlus)];
        _lineView.backgroundColor = LCHexColor(0xEBE5E1);
    }
    return _lineView;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self.contentView addSubview:self.bookIconImageView];
//        [self.contentView addSubview:self.UnReadPointImageView];
        [self.contentView addSubview:self.bookTitleLabel];
        [self.contentView addSubview:self.bookAuthorAndChapterLabel];
        [self.contentView addSubview:self.addToBook];
        [self.contentView addSubview:self.bookUpdateTimeLabel];
        [self.contentView addSubview:self.lineView];
        
    }
    return self;
}

#pragma mark - 获取数据
- (void)getBookDetail:(HWReadRecordModel *)model andTag:(NSInteger)tag
{
    
    if ([model.likeStaus integerValue] == 1)
    {
        [self.addToBook setTitle:@"已加入" forState:UIControlStateNormal];
        [self.addToBook setTitleColor:LCHexColor(0xFFFFFF) forState:UIControlStateNormal];
        [self.addToBook setBackgroundColor:LCHexColor(0xCCCCCC)];
        self.addToBook.layer.borderColor = LCHexColor(0xCCCCCC).CGColor;
        self.addToBook.enabled = NO;
    }
    
    self.addToBook.tag = tag;
    
    [self.bookIconImageView sd_setImageWithURL:[NSURL URLWithString:model.bookCover] placeholderImage:[UIImage imageNamed:@"bookCommon"]];
    
    UIColor *bookTitleColor = nil;
    NSString *bookTitleStr = model.bookName;
    if ([model.bookStatus integerValue] == 1)
    {
        bookTitleStr = [NSString stringWithFormat:@"%@ 连载中",bookTitleStr];
        bookTitleColor = LCHexColor(0x49BFA5);
    }
    else
    {
        bookTitleStr = [NSString stringWithFormat:@"%@ 已完结",bookTitleStr];
        bookTitleColor = LCHexColor(0xFF771A);

    }
    
    self.bookTitleLabel.attributedText = [bookTitleStr addAttriButedString:bookTitleStr Color:bookTitleColor Font:[UIFont systemFontOfSize:10*kUIScaleBasePlus] rang:NSMakeRange(bookTitleStr.length-3,3)];
    
    
//    NSString *bookAuthorAndChapterStr = dic[@"bookAuthor"];
//    self.bookAuthorAndChapterLabel.attributedText = [bookAuthorAndChapterStr addAttriButedString:bookAuthorAndChapterStr Color:LCHexColor(0x0A0509) Font:[UIFont systemFontOfSize:13*kUIScaleBasePlus] rang:NSMakeRange(4,bookAuthorAndChapterStr.length-4)];
    self.bookAuthorAndChapterLabel.text = model.authorName;
    
    self.bookUpdateTimeLabel.text = model.updateTime;
}

- (void)addBook:(UIButton *)sender
{
    
    [sender setTitle:@"已加入" forState:UIControlStateNormal];
    [sender setTitleColor:LCHexColor(0xFFFFFF) forState:UIControlStateNormal];
    [sender setBackgroundColor:LCHexColor(0xCCCCCC)];
    sender.layer.borderColor = LCHexColor(0xCCCCCC).CGColor;
    sender.enabled = NO;
    
    if (self.getBackAddNumBlock)
    {
        self.getBackAddNumBlock([NSString stringWithFormat:@"%ld",sender.tag]);
    }
}
@end
